package com.syni.mdd.sjb.community.base.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.syni.mdd.sjb.common.component.mybatis.community.entity.SjbQywxCompany;
import com.syni.mdd.sjb.common.component.utils.common.exception.BusinessException;
import com.syni.mdd.sjb.common.component.utils.common.utils.BeanContext;
import com.syni.mdd.sjb.common.component.utils.common.utils.StringUtils;
import com.syni.mdd.sjb.community.base.service.SjbQywxCompanyService;

import java.util.Objects;

/**
 * @author wangbaitao
 * @version 1.0.0
 * <h>公共操作工具类</h>
 * @Date 2021/2/26
 **/
public class CommonBeanUtils {
    private static final SjbQywxCompanyService sjbQywxCompanyService = BeanContext.getBean(SjbQywxCompanyService.class);

    public static String getToken(String corpId) {
        //获取永久授权码
        SjbQywxCompany company = sjbQywxCompanyService.getOne(new QueryWrapper<SjbQywxCompany>()
                .eq(SjbQywxCompany.CORP_ID, corpId));
        if (Objects.isNull(company)) {
            throw new BusinessException("业务异常：不存在该企业的授权信息");
        }
        //获取企业token
        String token = OAUtils.getAccessTokenByPerCode(company.getCorpId(), company.getPermanentCode());
        if (StringUtils.isBlank(token)) {
            throw new BusinessException("业务异常：token获取失败，请检查日志信息[关键字：accessToken获取失败]");
        }
        return token;
    }
}
